package com.mkb.service.impl;

import java.util.List;

import com.mkb.entity.UserInfoDO;
import com.mkb.entity.UserInfoDTO;
import com.mkb.utils.MapUtils;
import com.zz.common.util.ObjectUtils;
import com.zz.common.validator.Assert;
import com.zz.common.validator.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mkb.dao.UserInfoDAO;
import com.mkb.service.UserInfoService;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class UserInfoServiceImpl implements UserInfoService {
	@Autowired
	private UserInfoDAO userInfoDAO;

	@Override
	public Boolean deleteById(Long id) {
		userInfoDAO.deleteById(id);
		return true;
	}

	@Override
	public Boolean create(UserInfoDTO record) throws Exception {
		UserInfoDO entity = record.clone(UserInfoDO.class);
		userInfoDAO.insert(entity);
		return true;
	}

	@Override
	public UserInfoDTO getById(Long id) throws Exception{
		Assert.isNull(id, "id 不能为空");
		UserInfoDO entity= userInfoDAO.getById(id);
		Assert.isNull(entity, "对象不存在");
		UserInfoDTO dto = entity.clone(UserInfoDTO.class);
		return dto;
	}

	@Override
	public Boolean update(UserInfoDTO record) throws Exception {
		Assert.isNull(record, "对象不存在");
		Assert.isNull(record.getId(), "对象id不存在");
		UserInfoDO entity = record.clone(UserInfoDO.class);
		userInfoDAO.update(entity);
		return true;
	}

	@Override
	public PageInfo<UserInfoDTO> queryPage(UserInfoDTO userInfoDTO) throws Exception {
	        ValidatorUtils.validateProperty(userInfoDTO, "pageNum", "pageSize");
		PageHelper.startPage(userInfoDTO.getPageNum(), userInfoDTO.getPageSize());
		List<UserInfoDO> userInfoDOS = userInfoDAO.queryList(MapUtils.parseMap(userInfoDTO));
		List<UserInfoDTO> userInfoDTOList = ObjectUtils.convertList(userInfoDOS, UserInfoDTO.class);
		return new PageInfo<>(userInfoDTOList);
	}

	@Override
	public List<UserInfoDTO> queryList(UserInfoDTO userInfoDTO) throws Exception {
		List<UserInfoDO> userInfoDOS = userInfoDAO.queryList(MapUtils.parseMap(userInfoDTO));
		List<UserInfoDTO> userInfoDTOList = ObjectUtils.convertList(userInfoDOS, UserInfoDTO.class);
		return userInfoDTOList;
	}

}